<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="dialogVisible=true">新增角色</el-button>
            <!-- 表格 -->
            <el-table
              :data="roleData"
              border
              style="width: 100%; margin: 15px 0"
            >
              <el-table-column
                type="index"
                label="序号"
                width="50"
              />
              <el-table-column
                sortable
                prop="name"
                label="角色名"
                width="390"
              />
              <el-table-column
                prop="description"
                sortable
                label="描述"
                width="390"
              />
              <el-table-column
                width="390"
                label="操作"
              >
                <template slot-scope="{ row }">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 5, 10, 15]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-form ref="form" :model="company" label-width="80px">
              <!-- 开启栅格 -->
              <el-row>
                <el-col :offset="3" :span="8">
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地质">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增角色弹出 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        @close="addDialogClose"
      >
        <el-form ref="addRoleForm" :model="addRoleForm" :rules="addRoleRules" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="addRoleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="addRoleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSave">
            确 定
          </el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo } from '@/api/company'
import { getRolesList, addRolesList, editRoles } from '@/api/roles'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      company: {},
      roleData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      dialogVisible: false,
      addRoleForm: {
        name: '',
        description: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    ...mapGetters(['companyId'])
  },

  created() {
    this.loadCompanyInfo()
    this.loadRolesList()
  },

  methods: {
    // 加载公司信息
    async loadCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      console.log(res)
      this.company = res
    },
    // 加载角色信息
    async loadRolesList() {
      const res = await getRolesList(this.queryData)
      console.log(res)
      this.roleData = res.rows
      this.total = res.total
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.loadRolesList()
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.loadRolesList()
    },
    addDialogClose() {
      this.$refs.addRoleForm.resetFields()
      this.dialogVisible = false
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 点击确定
    async onSave() {
      try {
        await this.$refs.addRoleForm.validate()
        if (this.addRoleForm.id) {
          await editRoles(this.addRoleForm)
        } else {
          await addRolesList(this.addRoleForm)
        }
        this.dialogVisible = false
        this.loadRolesList()
      } catch (err) {
        console.log(err)
      }
    },
    onEdit(row) {
      this.dialogVisible = true
      this.addRoleForm = { ...row }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-tabs__item {
  width: 140px;
  text-align: center;
  font-size: 16px;
}
</style>
